#! /bin/sh
#
# Fetch rules and targets prior for the "njs" benchmark.
#
# rule_dir: input/zulip/???
# target_dir: input/zulip/???
#
# Uses sh because bash is not installed within the semgrep docker container.
#
set -eu

mkdir -p input

# This the original ruleset from zulip/tools/ minus the rule that requires
# '--dangerously-allow-arbitrary-code-execution-from-rules'
cp semgrep.yml input

cd input

# Obtain a shallow clone of a git repo for a specific commit
shallow_clone() {
  if [ -d "$name" ]; then
    echo "Reusing repo '$name'"
  else
    echo "Obtaining a shallow clone of git repo '$name', commit $commit"
    mkdir -p "$name"
    (
      cd "$name"
      git init
      git remote add origin "$url"
      git fetch --depth 1 origin "$commit"
      git checkout FETCH_HEAD -b tmp
    )
  fi
}

# Obtain semgrep rules and targets
name="zulip"
url="https://github.com/zulip/zulip.git"
commit="829f9272d2c4299a0c0a37a09802248d8136c0a8"
shallow_clone
